package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.common.utils.CommonResponse;
import com.hdax.dm.common.utils.ResponseUtil;
import com.hdax.dm.dtos.scheduler.SeatDto;
import com.hdax.dm.entity.scheduler.DmSchedulerSeat;
import com.hdax.dm.mappers.SchedulerSeatMapper;
import com.hdax.dm.service.SchedulerSeatService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SchedulerSeatServiceImpl extends ServiceImpl<SchedulerSeatMapper, DmSchedulerSeat> implements SchedulerSeatService {
    @Override
    public int getSaleCount(Long scheduleId, Long areaLevel) {
        return baseMapper.getSaleCount(scheduleId, areaLevel);
    }

    @Override
    public CommonResponse<SeatDto> getSeatArray(Long scheduleId) {
        QueryWrapper<DmSchedulerSeat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scheduleId",scheduleId);
        //所有的座位（某场演出） 65
        List<DmSchedulerSeat> dmSchedulerSeats = baseMapper.selectList(queryWrapper);;
        //根据x进行分组
        //x=1  List<y>
        //x=2  List<y>
        Map<Long, List<DmSchedulerSeat>> collect = dmSchedulerSeats.stream().collect(Collectors.groupingBy(DmSchedulerSeat::getX));
        SeatDto seatDto = new SeatDto();
        collect.keySet().forEach(x->{
            StringBuffer buffer = new StringBuffer();
            //每一排的所有座位
            List<DmSchedulerSeat> seats = collect.get(x);
            seats.forEach(seat->{
                if (seat.getAreaLevel() == 1L) {
                    buffer.append("a");
                }else if(seat.getAreaLevel() == 2L){
                    buffer.append("b");
                }else if(seat.getAreaLevel() == 3L){
                    buffer.append("c");
                }else{
                    buffer.append("_");
                }
            });
            seatDto.getSeatArray().add(buffer.toString());
        });
        return ResponseUtil.returnSuccess(seatDto);
    }

    @Override
    public boolean changeSeatByOrder(List<DmSchedulerSeat> parmas) {
        updateBatchById(parmas);
        return true;
    }

    @Override
    public boolean rollBackSeat(String orderNo) {
        return baseMapper.rollBackSeatByOrder(orderNo);
    }
}
